<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 创建函数 truncate(str, maxlength) 来检查 str 的长度，如果超过 maxlength —— 应使用 "…" 来代替 str 的结尾部分，长度仍然等于 maxlength。
        function truncate(str, maxlength) {
            if (str.length > maxlength) {
                str = str.slice(0, maxlength) + "…";
                console.log(str);
            }else if(str.length < maxlength){
                console.log(str);
            }
            return str;
        }
        // truncate(prompt('请输入一句话'),5);
        truncate("What I'd like to tell on this topic is:", 20); // "What I'd like to te…"
        truncate("Hi everyone!", 20); // "Hi everyone!"
    </script>
</body>

</html>